package com.gzy.literature.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.gzy.literature.entity.Users;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.gzy.literature.service.UsersService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServlet;
import java.util.Map;

@RestController
@RequestMapping("users")
public class UsersController extends HttpServlet {
    @Resource
    private UsersService usersService;

    @GetMapping("/page")
    public ResponseEntity<PageInfo<Users>> queryByPage(
            @RequestParam(required = false) String name,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        if (pageNum <= 0 || pageSize <= 0) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
        }
        Users users = new Users();
        users.setName(name);
        PageInfo<Users> page = usersService.queryAll(users, pageNum, pageSize);
        return ResponseEntity.ok(page);
    }
    @GetMapping("{id}")
    public ResponseEntity<Users> queryById(@PathVariable("id") Integer id) {
        if (id == null || id <= 0) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
        }
        Users user = usersService.queryById(id);
        if (user == null) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
        }
        return ResponseEntity.ok(user);
    }

    @PostMapping
    public ResponseEntity<Users> add(@RequestBody Users users) {
        if (users == null || users.getName() == null || users.getName().isEmpty() ||
                users.getPassword() == null || users.getPassword().isEmpty() ||
                users.getEmail() == null || !users.getEmail().contains("@")) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
        }
        Users createdUser = usersService.insert(users);
        return ResponseEntity.ok(createdUser);
    }

    @PutMapping
    public ResponseEntity<Users> edit(@RequestBody Users users) {
        if (users == null || users.getId() == null || users.getId() <= 0) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
        }
        if (users.getName() == null || users.getName().isEmpty()) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
        }
        if (users.getEmail() == null || !users.getEmail().contains("@")) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
        }
        if (users.getPassword() != null && users.getPassword().isEmpty()) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
        }
        Users updatedUser = usersService.update(users);
        if (updatedUser == null) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
        }
        return ResponseEntity.ok(updatedUser);
    }

    @DeleteMapping("{id}")
    public ResponseEntity<Boolean> deleteById(@PathVariable("id") Integer id) {
        if (id == null || id <= 0) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(false);
        }
        boolean success = usersService.deleteById(id);
        if (!success) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(false);
        }
        return ResponseEntity.ok(success);
    }
    @PostMapping("/login")
    public ResponseEntity<?> login(@RequestBody Map<String, String> loginInfo) {
        String account = loginInfo.get("account");
        String password = loginInfo.get("password");
        if (account == null || account.isEmpty() || password == null || password.isEmpty()) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Invalid login request.");
        }
        Users loggedInUser = usersService.login(account, password);
        if (loggedInUser != null) {
            return ResponseEntity.ok(loggedInUser);
        } else {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Invalid credentials or user not activated.");
        }
    }
    @GetMapping("/byUsername")
    public ResponseEntity<Users> queryByUsername(@RequestParam String username) {
        if (username == null || username.trim().isEmpty()) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
        }
        Users user = usersService.queryByUsername(username);
        if (user == null) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
        }
        return ResponseEntity.ok(user);
    }

    // New method to check if username exists
    @GetMapping("/checkUsername")
    public ResponseEntity<Boolean> checkUsernameExists(@RequestParam String username) {
        if (username == null || username.trim().isEmpty()) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(false);
        }
        boolean exists = usersService.isUsernameExist(username);
        return ResponseEntity.ok(exists);
    }
}
